﻿// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Threading;
using Microsoft.Xrm.Sdk.Query;

namespace Microsoft.Xrm.Sdk.Discovery
{
	[DebuggerStepThrough]
	[GeneratedCode("System.ServiceModel", "4.0.0.0")]
	public partial class ExecuteCompletedEventArgs : AsyncCompletedEventArgs
	{
		private readonly object[] results;

		public ExecuteCompletedEventArgs(object[] results, Exception exception, bool cancelled, object userState) :
			base(exception, cancelled, userState)
		{
			this.results = results;
		}

		public DiscoveryResponse Result
		{
			get
			{
				base.RaiseExceptionIfNecessary();
				return ((DiscoveryResponse)(results[0]));
			}
		}
	}

	[DebuggerStepThrough]
	[GeneratedCode("System.ServiceModel", "4.0.0.0")]
	public partial class DiscoveryServiceClient : ClientBase<IDiscoveryService>, IDiscoveryService
	{
		private BeginOperationDelegate onBeginCloseDelegate;
		private BeginOperationDelegate onBeginExecuteDelegate;

		private BeginOperationDelegate onBeginOpenDelegate;
		private SendOrPostCallback onCloseCompletedDelegate;
		private EndOperationDelegate onEndCloseDelegate;
		private EndOperationDelegate onEndExecuteDelegate;

		private EndOperationDelegate onEndOpenDelegate;
		private SendOrPostCallback onExecuteCompletedDelegate;

		private SendOrPostCallback onOpenCompletedDelegate;

		public DiscoveryServiceClient()
		{
		}

		public DiscoveryServiceClient(string endpointConfigurationName) :
			base(endpointConfigurationName)
		{
		}

		public DiscoveryServiceClient(string endpointConfigurationName, string remoteAddress) :
			base(endpointConfigurationName, remoteAddress)
		{
		}

		public DiscoveryServiceClient(string endpointConfigurationName, EndpointAddress remoteAddress) :
			base(endpointConfigurationName, remoteAddress)
		{
		}

		public DiscoveryServiceClient(Binding binding, EndpointAddress remoteAddress) :
			base(binding, remoteAddress)
		{
		}

		public CookieContainer CookieContainer
		{
			get
			{
				IHttpCookieContainerManager httpCookieContainerManager =
					InnerChannel.GetProperty<IHttpCookieContainerManager>();
				if ((httpCookieContainerManager != null))
				{
					return httpCookieContainerManager.CookieContainer;
				}
				else
				{
					return null;
				}
			}
			set
			{
				IHttpCookieContainerManager httpCookieContainerManager =
					InnerChannel.GetProperty<IHttpCookieContainerManager>();
				if ((httpCookieContainerManager != null))
				{
					httpCookieContainerManager.CookieContainer = value;
				}
				else
				{
					throw new InvalidOperationException(
						"Unable to set the CookieContainer. Please make sure the binding contains an HttpC" +
						"ookieContainerBindingElement.");
				}
			}
		}

		#region IDiscoveryService Members

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		IAsyncResult IDiscoveryService.BeginExecute(DiscoveryRequest request, AsyncCallback callback, object asyncState)
		{
			return base.Channel.BeginExecute(request, callback, asyncState);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		DiscoveryResponse IDiscoveryService.EndExecute(IAsyncResult result)
		{
			return base.Channel.EndExecute(result);
		}

		#endregion

		public event EventHandler<ExecuteCompletedEventArgs> ExecuteCompleted;

		public event EventHandler<AsyncCompletedEventArgs> OpenCompleted;

		public event EventHandler<AsyncCompletedEventArgs> CloseCompleted;

		private IAsyncResult OnBeginExecute(object[] inValues, AsyncCallback callback, object asyncState)
		{
			DiscoveryRequest request = ((DiscoveryRequest)(inValues[0]));
			return ((IDiscoveryService)(this)).BeginExecute(request, callback, asyncState);
		}

		private object[] OnEndExecute(IAsyncResult result)
		{
			DiscoveryResponse retVal = ((IDiscoveryService)(this)).EndExecute(result);
			return new object[]
                       {
                           retVal
                       };
		}

		private void OnExecuteCompleted(object state)
		{
			if ((ExecuteCompleted != null))
			{
				InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
				ExecuteCompleted(this, new ExecuteCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
			}
		}

		public void ExecuteAsync(DiscoveryRequest request)
		{
			ExecuteAsync(request, null);
		}

		public void ExecuteAsync(DiscoveryRequest request, object userState)
		{
			if ((onBeginExecuteDelegate == null))
			{
				onBeginExecuteDelegate = new BeginOperationDelegate(OnBeginExecute);
			}
			if ((onEndExecuteDelegate == null))
			{
				onEndExecuteDelegate = new EndOperationDelegate(OnEndExecute);
			}
			if ((onExecuteCompletedDelegate == null))
			{
				onExecuteCompletedDelegate = new SendOrPostCallback(OnExecuteCompleted);
			}
			base.InvokeAsync(onBeginExecuteDelegate, new object[]
                                                         {
                                                             request
                                                         }, onEndExecuteDelegate, onExecuteCompletedDelegate, userState);
		}

		private IAsyncResult OnBeginOpen(object[] inValues, AsyncCallback callback, object asyncState)
		{
			return ((ICommunicationObject)(this)).BeginOpen(callback, asyncState);
		}

		private object[] OnEndOpen(IAsyncResult result)
		{
			((ICommunicationObject)(this)).EndOpen(result);
			return null;
		}

		private void OnOpenCompleted(object state)
		{
			if ((OpenCompleted != null))
			{
				InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
				OpenCompleted(this, new AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
			}
		}

		public void OpenAsync()
		{
			OpenAsync(null);
		}

		public void OpenAsync(object userState)
		{
			if ((onBeginOpenDelegate == null))
			{
				onBeginOpenDelegate = new BeginOperationDelegate(OnBeginOpen);
			}
			if ((onEndOpenDelegate == null))
			{
				onEndOpenDelegate = new EndOperationDelegate(OnEndOpen);
			}
			if ((onOpenCompletedDelegate == null))
			{
				onOpenCompletedDelegate = new SendOrPostCallback(OnOpenCompleted);
			}
			base.InvokeAsync(onBeginOpenDelegate, null, onEndOpenDelegate, onOpenCompletedDelegate, userState);
		}

		private IAsyncResult OnBeginClose(object[] inValues, AsyncCallback callback, object asyncState)
		{
			return ((ICommunicationObject)(this)).BeginClose(callback, asyncState);
		}

		private object[] OnEndClose(IAsyncResult result)
		{
			((ICommunicationObject)(this)).EndClose(result);
			return null;
		}

		private void OnCloseCompleted(object state)
		{
			if ((CloseCompleted != null))
			{
				InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
				CloseCompleted(this, new AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
			}
		}

		public void CloseAsync()
		{
			CloseAsync(null);
		}

		public void CloseAsync(object userState)
		{
			if ((onBeginCloseDelegate == null))
			{
				onBeginCloseDelegate = new BeginOperationDelegate(OnBeginClose);
			}
			if ((onEndCloseDelegate == null))
			{
				onEndCloseDelegate = new EndOperationDelegate(OnEndClose);
			}
			if ((onCloseCompletedDelegate == null))
			{
				onCloseCompletedDelegate = new SendOrPostCallback(OnCloseCompleted);
			}
			base.InvokeAsync(onBeginCloseDelegate, null, onEndCloseDelegate, onCloseCompletedDelegate, userState);
		}

		protected override IDiscoveryService CreateChannel()
		{
			return new DiscoveryServiceClientChannel(this);
		}

		#region Nested type: DiscoveryServiceClientChannel

		private class DiscoveryServiceClientChannel : ChannelBase<IDiscoveryService>, IDiscoveryService
		{
			public DiscoveryServiceClientChannel(ClientBase<IDiscoveryService> client) :
				base(client)
			{
			}

			#region IDiscoveryService Members

			public IAsyncResult BeginExecute(DiscoveryRequest request, AsyncCallback callback, object asyncState)
			{
				object[] _args = new object[1];
				_args[0] = request;
				IAsyncResult _result = base.BeginInvoke("Execute", _args, callback, asyncState);
				return _result;
			}

			public DiscoveryResponse EndExecute(IAsyncResult result)
			{
				object[] _args = new object[0];
				DiscoveryResponse _result = ((DiscoveryResponse)(base.EndInvoke("Execute", _args, result)));
				return _result;
			}

			#endregion
		}

		#endregion
	}

}

namespace Microsoft.Xrm.Sdk.Discovery
{
	using System;
	using System.Runtime.Serialization;

	#region Generic collection items

	[DataContract(Name = "KeyValuePairOfEndpointTypestringztYlk6OT",
		Namespace = "http://schemas.datacontract.org/2004/07/System.Collections.Generic")]
	public class KeyValuePairOfEndpointTypestringztYlk6OT
	{
		//public KeyValuePairOfEndpointTypestringztYlk6OT() { }

		[DataMember(Name = "key")]
		public EndpointType Key { get; set; }

		[DataMember(Name = "value")]
		public String Value { get; set; }
	}

	#endregion Generic collection items

}




namespace Microsoft.Xrm.Sdk
{
	[GeneratedCode("System.ServiceModel", "4.0.0.0")]
	[ServiceContract(Namespace = Microsoft.Xrm.Sdk.XmlNamespaces.V5.Services,
		ConfigurationName = "Microsoft.Xrm.Sdk.IOrganizationService")]
	public interface IOrganizationService
	{
		[OperationContract(AsyncPattern = true)]
		[FaultContract(typeof(OrganizationServiceFault))]
		IAsyncResult BeginCreate(Entity entity, AsyncCallback callback, object asyncState);

		Guid EndCreate(IAsyncResult result);

		[OperationContract(AsyncPattern = true)]
		[FaultContract(typeof(OrganizationServiceFault))]
		IAsyncResult BeginRetrieve(string entityName, Guid id, ColumnSet columnSet, AsyncCallback callback,
								   object asyncState);

		Entity EndRetrieve(IAsyncResult result);

		[OperationContract(AsyncPattern = true)]
		[FaultContract(typeof(OrganizationServiceFault))]
		IAsyncResult BeginUpdate(Entity entity, AsyncCallback callback, object asyncState);

		void EndUpdate(IAsyncResult result);

		[OperationContract(AsyncPattern = true)]
		[FaultContract(typeof(OrganizationServiceFault))]
		IAsyncResult BeginDelete(string entityName, Guid id, AsyncCallback callback, object asyncState);

		void EndDelete(IAsyncResult result);

		[OperationContract(AsyncPattern = true)]
		[FaultContract(typeof(OrganizationServiceFault))]
		IAsyncResult BeginExecute(OrganizationRequest request, AsyncCallback callback, object asyncState);

		OrganizationResponse EndExecute(IAsyncResult result);

		[OperationContract(AsyncPattern = true)]
		[FaultContract(typeof(OrganizationServiceFault))]
		IAsyncResult BeginAssociate(string entityName, Guid entityId, Relationship relationship,
									EntityReferenceCollection relatedEntities, AsyncCallback callback, object asyncState);

		void EndAssociate(IAsyncResult result);

		[OperationContract(AsyncPattern = true)]
		[FaultContract(typeof(OrganizationServiceFault))]
		IAsyncResult BeginDisassociate(string entityName, Guid entityId, Relationship relationship,
									   EntityReferenceCollection relatedEntities, AsyncCallback callback,
									   object asyncState);

		void EndDisassociate(IAsyncResult result);

		[OperationContract(AsyncPattern = true)]
		[FaultContract(typeof(OrganizationServiceFault))]
		IAsyncResult BeginRetrieveMultiple(QueryBase query, AsyncCallback callback, object asyncState);

		EntityCollection EndRetrieveMultiple(IAsyncResult result);
	}


	[GeneratedCode("System.ServiceModel", "4.0.0.0")]
	public interface IOrganizationServiceChannel : IOrganizationService, IClientChannel
	{
	}


	[GeneratedCode("System.ServiceModel", "4.0.0.0")]
	public partial class CreateCompletedEventArgs : AsyncCompletedEventArgs
	{
		private readonly object[] results;

		public CreateCompletedEventArgs(object[] results, Exception exception, bool cancelled, object userState) :
			base(exception, cancelled, userState)
		{
			this.results = results;
		}

		public Guid Result
		{
			get
			{
				base.RaiseExceptionIfNecessary();
				return ((Guid)(results[0]));
			}
		}
	}


	[GeneratedCode("System.ServiceModel", "4.0.0.0")]
	public partial class RetrieveCompletedEventArgs : AsyncCompletedEventArgs
	{
		private readonly object[] results;

		public RetrieveCompletedEventArgs(object[] results, Exception exception, bool cancelled, object userState) :
			base(exception, cancelled, userState)
		{
			this.results = results;
		}

		public Entity Result
		{
			get
			{
				base.RaiseExceptionIfNecessary();
				return ((Entity)(results[0]));
			}
		}
	}


	[GeneratedCode("System.ServiceModel", "4.0.0.0")]
	public partial class ExecuteCompletedEventArgs : AsyncCompletedEventArgs
	{
		private readonly object[] results;

		public ExecuteCompletedEventArgs(object[] results, Exception exception, bool cancelled, object userState) :
			base(exception, cancelled, userState)
		{
			this.results = results;
		}

		public OrganizationResponse Result
		{
			get
			{
				base.RaiseExceptionIfNecessary();
				return ((OrganizationResponse)(results[0]));
			}
		}
	}


	[GeneratedCode("System.ServiceModel", "4.0.0.0")]
	public partial class RetrieveMultipleCompletedEventArgs : AsyncCompletedEventArgs
	{
		private readonly object[] results;

		public RetrieveMultipleCompletedEventArgs(object[] results, Exception exception, bool cancelled,
												  object userState) :
			base(exception, cancelled, userState)
		{
			this.results = results;
		}

		public EntityCollection Result
		{
			get
			{
				base.RaiseExceptionIfNecessary();
				return ((EntityCollection)(results[0]));
			}
		}
	}

	[GeneratedCode("System.ServiceModel", "4.0.0.0")]
	public partial class OrganizationServiceClient : ClientBase<IOrganizationService>, IOrganizationService
	{
		private SendOrPostCallback onAssociateCompletedDelegate;
		private BeginOperationDelegate onBeginAssociateDelegate;
		private BeginOperationDelegate onBeginCloseDelegate;
		private BeginOperationDelegate onBeginCreateDelegate;
		private BeginOperationDelegate onBeginDeleteDelegate;
		private BeginOperationDelegate onBeginDisassociateDelegate;
		private BeginOperationDelegate onBeginExecuteDelegate;
		private BeginOperationDelegate onBeginOpenDelegate;

		private BeginOperationDelegate onBeginRetrieveDelegate;
		private BeginOperationDelegate onBeginRetrieveMultipleDelegate;

		private BeginOperationDelegate onBeginUpdateDelegate;
		private SendOrPostCallback onCloseCompletedDelegate;
		private SendOrPostCallback onCreateCompletedDelegate;

		private SendOrPostCallback onDeleteCompletedDelegate;
		private SendOrPostCallback onDisassociateCompletedDelegate;

		private EndOperationDelegate onEndAssociateDelegate;
		private EndOperationDelegate onEndCloseDelegate;
		private EndOperationDelegate onEndCreateDelegate;
		private EndOperationDelegate onEndDeleteDelegate;

		private EndOperationDelegate onEndDisassociateDelegate;
		private EndOperationDelegate onEndExecuteDelegate;

		private EndOperationDelegate onEndOpenDelegate;
		private EndOperationDelegate onEndRetrieveDelegate;
		private EndOperationDelegate onEndRetrieveMultipleDelegate;
		private EndOperationDelegate onEndUpdateDelegate;
		private SendOrPostCallback onExecuteCompletedDelegate;

		private SendOrPostCallback onOpenCompletedDelegate;
		private SendOrPostCallback onRetrieveCompletedDelegate;
		private SendOrPostCallback onRetrieveMultipleCompletedDelegate;
		private SendOrPostCallback onUpdateCompletedDelegate;

		public OrganizationServiceClient()
		{
		}

		public OrganizationServiceClient(string endpointConfigurationName) :
			base(endpointConfigurationName)
		{
		}

		public OrganizationServiceClient(string endpointConfigurationName, string remoteAddress) :
			base(endpointConfigurationName, remoteAddress)
		{
		}

		public OrganizationServiceClient(string endpointConfigurationName, EndpointAddress remoteAddress) :
			base(endpointConfigurationName, remoteAddress)
		{
		}

		public OrganizationServiceClient(Binding binding, EndpointAddress remoteAddress) :
			base(binding, remoteAddress)
		{
		}

		public CookieContainer CookieContainer
		{
			get
			{
				IHttpCookieContainerManager httpCookieContainerManager =
					InnerChannel.GetProperty<IHttpCookieContainerManager>();
				if ((httpCookieContainerManager != null))
				{
					return httpCookieContainerManager.CookieContainer;
				}
				else
				{
					return null;
				}
			}
			set
			{
				IHttpCookieContainerManager httpCookieContainerManager =
					InnerChannel.GetProperty<IHttpCookieContainerManager>();
				if ((httpCookieContainerManager != null))
				{
					httpCookieContainerManager.CookieContainer = value;
				}
				else
				{
					throw new InvalidOperationException(
						"Unable to set the CookieContainer. Please make sure the binding contains an HttpC" +
						"ookieContainerBindingElement.");
				}
			}
		}

		#region IOrganizationService Members

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		IAsyncResult IOrganizationService.BeginCreate(Entity entity, AsyncCallback callback, object asyncState)
		{
			return base.Channel.BeginCreate(entity, callback, asyncState);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		Guid IOrganizationService.EndCreate(IAsyncResult result)
		{
			return base.Channel.EndCreate(result);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		IAsyncResult IOrganizationService.BeginRetrieve(string entityName, Guid id, ColumnSet columnSet,
														AsyncCallback callback, object asyncState)
		{
			return base.Channel.BeginRetrieve(entityName, id, columnSet, callback, asyncState);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		Entity IOrganizationService.EndRetrieve(IAsyncResult result)
		{
			return base.Channel.EndRetrieve(result);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		IAsyncResult IOrganizationService.BeginUpdate(Entity entity, AsyncCallback callback, object asyncState)
		{
			return base.Channel.BeginUpdate(entity, callback, asyncState);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		void IOrganizationService.EndUpdate(IAsyncResult result)
		{
			base.Channel.EndUpdate(result);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		IAsyncResult IOrganizationService.BeginDelete(string entityName, Guid id, AsyncCallback callback,
													  object asyncState)
		{
			return base.Channel.BeginDelete(entityName, id, callback, asyncState);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		void IOrganizationService.EndDelete(IAsyncResult result)
		{
			base.Channel.EndDelete(result);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		IAsyncResult IOrganizationService.BeginExecute(OrganizationRequest request, AsyncCallback callback,
													   object asyncState)
		{
			return base.Channel.BeginExecute(request, callback, asyncState);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		OrganizationResponse IOrganizationService.EndExecute(IAsyncResult result)
		{
			return base.Channel.EndExecute(result);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		IAsyncResult IOrganizationService.BeginAssociate(string entityName, Guid entityId, Relationship relationship,
														 EntityReferenceCollection relatedEntities,
														 AsyncCallback callback, object asyncState)
		{
			return base.Channel.BeginAssociate(entityName, entityId, relationship, relatedEntities, callback, asyncState);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		void IOrganizationService.EndAssociate(IAsyncResult result)
		{
			base.Channel.EndAssociate(result);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		IAsyncResult IOrganizationService.BeginDisassociate(string entityName, Guid entityId, Relationship relationship,
															EntityReferenceCollection relatedEntities,
															AsyncCallback callback, object asyncState)
		{
			return base.Channel.BeginDisassociate(entityName, entityId, relationship, relatedEntities, callback,
												  asyncState);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		void IOrganizationService.EndDisassociate(IAsyncResult result)
		{
			base.Channel.EndDisassociate(result);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		IAsyncResult IOrganizationService.BeginRetrieveMultiple(QueryBase query, AsyncCallback callback,
																object asyncState)
		{
			return base.Channel.BeginRetrieveMultiple(query, callback, asyncState);
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		EntityCollection IOrganizationService.EndRetrieveMultiple(IAsyncResult result)
		{
			return base.Channel.EndRetrieveMultiple(result);
		}

		#endregion

		public event EventHandler<CreateCompletedEventArgs> CreateCompleted;

		public event EventHandler<RetrieveCompletedEventArgs> RetrieveCompleted;

		public event EventHandler<AsyncCompletedEventArgs> UpdateCompleted;

		public event EventHandler<AsyncCompletedEventArgs> DeleteCompleted;

		public event EventHandler<ExecuteCompletedEventArgs> ExecuteCompleted;

		public event EventHandler<AsyncCompletedEventArgs> AssociateCompleted;

		public event EventHandler<AsyncCompletedEventArgs> DisassociateCompleted;

		public event EventHandler<RetrieveMultipleCompletedEventArgs> RetrieveMultipleCompleted;

		public event EventHandler<AsyncCompletedEventArgs> OpenCompleted;

		public event EventHandler<AsyncCompletedEventArgs> CloseCompleted;

		private IAsyncResult OnBeginCreate(object[] inValues, AsyncCallback callback, object asyncState)
		{
			Entity entity = ((Entity)(inValues[0]));
			return ((IOrganizationService)(this)).BeginCreate(entity, callback, asyncState);
		}

		private object[] OnEndCreate(IAsyncResult result)
		{
			Guid retVal = ((IOrganizationService)(this)).EndCreate(result);
			return new object[]
                       {
                           retVal
                       };
		}

		private void OnCreateCompleted(object state)
		{
			if ((CreateCompleted != null))
			{
				InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
				CreateCompleted(this, new CreateCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
			}
		}

		public void CreateAsync(Entity entity)
		{
			CreateAsync(entity, null);
		}

		public void CreateAsync(Entity entity, object userState)
		{
			if ((onBeginCreateDelegate == null))
			{
				onBeginCreateDelegate = new BeginOperationDelegate(OnBeginCreate);
			}
			if ((onEndCreateDelegate == null))
			{
				onEndCreateDelegate = new EndOperationDelegate(OnEndCreate);
			}
			if ((onCreateCompletedDelegate == null))
			{
				onCreateCompletedDelegate = new SendOrPostCallback(OnCreateCompleted);
			}
			base.InvokeAsync(onBeginCreateDelegate, new object[]
                                                        {
                                                            entity
                                                        }, onEndCreateDelegate, onCreateCompletedDelegate, userState);
		}

		private IAsyncResult OnBeginRetrieve(object[] inValues, AsyncCallback callback, object asyncState)
		{
			string entityName = ((string)(inValues[0]));
			Guid id = ((Guid)(inValues[1]));
			ColumnSet columnSet = ((ColumnSet)(inValues[2]));
			return ((IOrganizationService)(this)).BeginRetrieve(entityName, id, columnSet, callback, asyncState);
		}

		private object[] OnEndRetrieve(IAsyncResult result)
		{
			Entity retVal = ((IOrganizationService)(this)).EndRetrieve(result);
			return new object[]
                       {
                           retVal
                       };
		}

		private void OnRetrieveCompleted(object state)
		{
			if ((RetrieveCompleted != null))
			{
				InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
				RetrieveCompleted(this, new RetrieveCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
			}
		}

		public void RetrieveAsync(string entityName, Guid id, ColumnSet columnSet)
		{
			RetrieveAsync(entityName, id, columnSet, null);
		}

		public void RetrieveAsync(string entityName, Guid id, ColumnSet columnSet, object userState)
		{
			if ((onBeginRetrieveDelegate == null))
			{
				onBeginRetrieveDelegate = new BeginOperationDelegate(OnBeginRetrieve);
			}
			if ((onEndRetrieveDelegate == null))
			{
				onEndRetrieveDelegate = new EndOperationDelegate(OnEndRetrieve);
			}
			if ((onRetrieveCompletedDelegate == null))
			{
				onRetrieveCompletedDelegate = new SendOrPostCallback(OnRetrieveCompleted);
			}
			base.InvokeAsync(onBeginRetrieveDelegate, new object[]
                                                          {
                                                              entityName,
                                                              id,
                                                              columnSet
                                                          }, onEndRetrieveDelegate, onRetrieveCompletedDelegate,
							 userState);
		}

		private IAsyncResult OnBeginUpdate(object[] inValues, AsyncCallback callback, object asyncState)
		{
			Entity entity = ((Entity)(inValues[0]));
			return ((IOrganizationService)(this)).BeginUpdate(entity, callback, asyncState);
		}

		private object[] OnEndUpdate(IAsyncResult result)
		{
			((IOrganizationService)(this)).EndUpdate(result);
			return null;
		}

		private void OnUpdateCompleted(object state)
		{
			if ((UpdateCompleted != null))
			{
				InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
				UpdateCompleted(this, new AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
			}
		}

		public void UpdateAsync(Entity entity)
		{
			UpdateAsync(entity, null);
		}

		public void UpdateAsync(Entity entity, object userState)
		{
			if ((onBeginUpdateDelegate == null))
			{
				onBeginUpdateDelegate = new BeginOperationDelegate(OnBeginUpdate);
			}
			if ((onEndUpdateDelegate == null))
			{
				onEndUpdateDelegate = new EndOperationDelegate(OnEndUpdate);
			}
			if ((onUpdateCompletedDelegate == null))
			{
				onUpdateCompletedDelegate = new SendOrPostCallback(OnUpdateCompleted);
			}
			base.InvokeAsync(onBeginUpdateDelegate, new object[]
                                                        {
                                                            entity
                                                        }, onEndUpdateDelegate, onUpdateCompletedDelegate, userState);
		}

		private IAsyncResult OnBeginDelete(object[] inValues, AsyncCallback callback, object asyncState)
		{
			string entityName = ((string)(inValues[0]));
			Guid id = ((Guid)(inValues[1]));
			return ((IOrganizationService)(this)).BeginDelete(entityName, id, callback, asyncState);
		}

		private object[] OnEndDelete(IAsyncResult result)
		{
			((IOrganizationService)(this)).EndDelete(result);
			return null;
		}

		private void OnDeleteCompleted(object state)
		{
			if ((DeleteCompleted != null))
			{
				InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
				DeleteCompleted(this, new AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
			}
		}

		public void DeleteAsync(string entityName, Guid id)
		{
			DeleteAsync(entityName, id, null);
		}

		public void DeleteAsync(string entityName, Guid id, object userState)
		{
			if ((onBeginDeleteDelegate == null))
			{
				onBeginDeleteDelegate = new BeginOperationDelegate(OnBeginDelete);
			}
			if ((onEndDeleteDelegate == null))
			{
				onEndDeleteDelegate = new EndOperationDelegate(OnEndDelete);
			}
			if ((onDeleteCompletedDelegate == null))
			{
				onDeleteCompletedDelegate = new SendOrPostCallback(OnDeleteCompleted);
			}
			base.InvokeAsync(onBeginDeleteDelegate, new object[]
                                                        {
                                                            entityName,
                                                            id
                                                        }, onEndDeleteDelegate, onDeleteCompletedDelegate, userState);
		}

		private IAsyncResult OnBeginExecute(object[] inValues, AsyncCallback callback, object asyncState)
		{
			OrganizationRequest request = ((OrganizationRequest)(inValues[0]));
			return ((IOrganizationService)(this)).BeginExecute(request, callback, asyncState);
		}

		private object[] OnEndExecute(IAsyncResult result)
		{
			OrganizationResponse retVal = ((IOrganizationService)(this)).EndExecute(result);
			return new object[]
                       {
                           retVal
                       };
		}

		private void OnExecuteCompleted(object state)
		{
			if ((ExecuteCompleted != null))
			{
				InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
				ExecuteCompleted(this, new ExecuteCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
			}
		}

		public void ExecuteAsync(OrganizationRequest request)
		{
			ExecuteAsync(request, null);
		}

		public void ExecuteAsync(OrganizationRequest request, object userState)
		{
			if ((onBeginExecuteDelegate == null))
			{
				onBeginExecuteDelegate = new BeginOperationDelegate(OnBeginExecute);
			}
			if ((onEndExecuteDelegate == null))
			{
				onEndExecuteDelegate = new EndOperationDelegate(OnEndExecute);
			}
			if ((onExecuteCompletedDelegate == null))
			{
				onExecuteCompletedDelegate = new SendOrPostCallback(OnExecuteCompleted);
			}
			base.InvokeAsync(onBeginExecuteDelegate, new object[]
                                                         {
                                                             request
                                                         }, onEndExecuteDelegate, onExecuteCompletedDelegate, userState);
		}

		private IAsyncResult OnBeginAssociate(object[] inValues, AsyncCallback callback, object asyncState)
		{
			string entityName = ((string)(inValues[0]));
			Guid entityId = ((Guid)(inValues[1]));
			Relationship relationship = ((Relationship)(inValues[2]));
			EntityReferenceCollection relatedEntities = ((EntityReferenceCollection)(inValues[3]));
			return ((IOrganizationService)(this)).BeginAssociate(entityName, entityId, relationship, relatedEntities,
																  callback, asyncState);
		}

		private object[] OnEndAssociate(IAsyncResult result)
		{
			((IOrganizationService)(this)).EndAssociate(result);
			return null;
		}

		private void OnAssociateCompleted(object state)
		{
			if ((AssociateCompleted != null))
			{
				InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
				AssociateCompleted(this, new AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
			}
		}

		public void AssociateAsync(string entityName, Guid entityId, Relationship relationship,
								   EntityReferenceCollection relatedEntities)
		{
			AssociateAsync(entityName, entityId, relationship, relatedEntities, null);
		}

		public void AssociateAsync(string entityName, Guid entityId, Relationship relationship,
								   EntityReferenceCollection relatedEntities, object userState)
		{
			if ((onBeginAssociateDelegate == null))
			{
				onBeginAssociateDelegate = new BeginOperationDelegate(OnBeginAssociate);
			}
			if ((onEndAssociateDelegate == null))
			{
				onEndAssociateDelegate = new EndOperationDelegate(OnEndAssociate);
			}
			if ((onAssociateCompletedDelegate == null))
			{
				onAssociateCompletedDelegate = new SendOrPostCallback(OnAssociateCompleted);
			}
			base.InvokeAsync(onBeginAssociateDelegate, new object[]
                                                           {
                                                               entityName,
                                                               entityId,
                                                               relationship,
                                                               relatedEntities
                                                           }, onEndAssociateDelegate, onAssociateCompletedDelegate,
							 userState);
		}

		private IAsyncResult OnBeginDisassociate(object[] inValues, AsyncCallback callback, object asyncState)
		{
			string entityName = ((string)(inValues[0]));
			Guid entityId = ((Guid)(inValues[1]));
			Relationship relationship = ((Relationship)(inValues[2]));
			EntityReferenceCollection relatedEntities = ((EntityReferenceCollection)(inValues[3]));
			return ((IOrganizationService)(this)).BeginDisassociate(entityName, entityId, relationship, relatedEntities,
																	 callback, asyncState);
		}

		private object[] OnEndDisassociate(IAsyncResult result)
		{
			((IOrganizationService)(this)).EndDisassociate(result);
			return null;
		}

		private void OnDisassociateCompleted(object state)
		{
			if ((DisassociateCompleted != null))
			{
				InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
				DisassociateCompleted(this, new AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
			}
		}

		public void DisassociateAsync(string entityName, Guid entityId, Relationship relationship,
									  EntityReferenceCollection relatedEntities)
		{
			DisassociateAsync(entityName, entityId, relationship, relatedEntities, null);
		}

		public void DisassociateAsync(string entityName, Guid entityId, Relationship relationship,
									  EntityReferenceCollection relatedEntities, object userState)
		{
			if ((onBeginDisassociateDelegate == null))
			{
				onBeginDisassociateDelegate = new BeginOperationDelegate(OnBeginDisassociate);
			}
			if ((onEndDisassociateDelegate == null))
			{
				onEndDisassociateDelegate = new EndOperationDelegate(OnEndDisassociate);
			}
			if ((onDisassociateCompletedDelegate == null))
			{
				onDisassociateCompletedDelegate = new SendOrPostCallback(OnDisassociateCompleted);
			}
			base.InvokeAsync(onBeginDisassociateDelegate, new object[]
                                                              {
                                                                  entityName,
                                                                  entityId,
                                                                  relationship,
                                                                  relatedEntities
                                                              }, onEndDisassociateDelegate,
							 onDisassociateCompletedDelegate, userState);
		}

		private IAsyncResult OnBeginRetrieveMultiple(object[] inValues, AsyncCallback callback, object asyncState)
		{
			QueryBase query = ((QueryBase)(inValues[0]));
			return ((IOrganizationService)(this)).BeginRetrieveMultiple(query, callback, asyncState);
		}

		private object[] OnEndRetrieveMultiple(IAsyncResult result)
		{
			EntityCollection retVal = ((IOrganizationService)(this)).EndRetrieveMultiple(result);
			return new object[]
                       {
                           retVal
                       };
		}

		private void OnRetrieveMultipleCompleted(object state)
		{
			if ((RetrieveMultipleCompleted != null))
			{
				InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
				RetrieveMultipleCompleted(this,
										  new RetrieveMultipleCompletedEventArgs(e.Results, e.Error, e.Cancelled,
																				 e.UserState));
			}
		}

		public void RetrieveMultipleAsync(QueryBase query)
		{
			RetrieveMultipleAsync(query, null);
		}

		public void RetrieveMultipleAsync(QueryBase query, object userState)
		{
			if ((onBeginRetrieveMultipleDelegate == null))
			{
				onBeginRetrieveMultipleDelegate = new BeginOperationDelegate(OnBeginRetrieveMultiple);
			}
			if ((onEndRetrieveMultipleDelegate == null))
			{
				onEndRetrieveMultipleDelegate = new EndOperationDelegate(OnEndRetrieveMultiple);
			}
			if ((onRetrieveMultipleCompletedDelegate == null))
			{
				onRetrieveMultipleCompletedDelegate = new SendOrPostCallback(OnRetrieveMultipleCompleted);
			}
			base.InvokeAsync(onBeginRetrieveMultipleDelegate, new object[]
                                                                  {
                                                                      query
                                                                  }, onEndRetrieveMultipleDelegate,
							 onRetrieveMultipleCompletedDelegate, userState);
		}

		private IAsyncResult OnBeginOpen(object[] inValues, AsyncCallback callback, object asyncState)
		{
			return ((ICommunicationObject)(this)).BeginOpen(callback, asyncState);
		}

		private object[] OnEndOpen(IAsyncResult result)
		{
			((ICommunicationObject)(this)).EndOpen(result);
			return null;
		}

		private void OnOpenCompleted(object state)
		{
			if ((OpenCompleted != null))
			{
				InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
				OpenCompleted(this, new AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
			}
		}

		public void OpenAsync()
		{
			OpenAsync(null);
		}

		public void OpenAsync(object userState)
		{
			if ((onBeginOpenDelegate == null))
			{
				onBeginOpenDelegate = new BeginOperationDelegate(OnBeginOpen);
			}
			if ((onEndOpenDelegate == null))
			{
				onEndOpenDelegate = new EndOperationDelegate(OnEndOpen);
			}
			if ((onOpenCompletedDelegate == null))
			{
				onOpenCompletedDelegate = new SendOrPostCallback(OnOpenCompleted);
			}
			base.InvokeAsync(onBeginOpenDelegate, null, onEndOpenDelegate, onOpenCompletedDelegate, userState);
		}

		private IAsyncResult OnBeginClose(object[] inValues, AsyncCallback callback, object asyncState)
		{
			return ((ICommunicationObject)(this)).BeginClose(callback, asyncState);
		}

		private object[] OnEndClose(IAsyncResult result)
		{
			((ICommunicationObject)(this)).EndClose(result);
			return null;
		}

		private void OnCloseCompleted(object state)
		{
			if ((CloseCompleted != null))
			{
				InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
				CloseCompleted(this, new AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
			}
		}

		public void CloseAsync()
		{
			CloseAsync(null);
		}

		public void CloseAsync(object userState)
		{
			if ((onBeginCloseDelegate == null))
			{
				onBeginCloseDelegate = new BeginOperationDelegate(OnBeginClose);
			}
			if ((onEndCloseDelegate == null))
			{
				onEndCloseDelegate = new EndOperationDelegate(OnEndClose);
			}
			if ((onCloseCompletedDelegate == null))
			{
				onCloseCompletedDelegate = new SendOrPostCallback(OnCloseCompleted);
			}
			base.InvokeAsync(onBeginCloseDelegate, null, onEndCloseDelegate, onCloseCompletedDelegate, userState);
		}

		protected override IOrganizationService CreateChannel()
		{
			return new OrganizationServiceClientChannel(this);
		}

		#region Nested type: OrganizationServiceClientChannel

		private class OrganizationServiceClientChannel : ChannelBase<IOrganizationService>, IOrganizationService
		{
			public OrganizationServiceClientChannel(ClientBase<IOrganizationService> client) :
				base(client)
			{
			}

			#region IOrganizationService Members

			public IAsyncResult BeginCreate(Entity entity, AsyncCallback callback, object asyncState)
			{
				object[] _args = new object[1];
				_args[0] = entity;
				IAsyncResult _result = base.BeginInvoke("Create", _args, callback, asyncState);
				return _result;
			}

			public Guid EndCreate(IAsyncResult result)
			{
				object[] _args = new object[0];
				Guid _result = ((Guid)(base.EndInvoke("Create", _args, result)));
				return _result;
			}

			public IAsyncResult BeginRetrieve(string entityName, Guid id, ColumnSet columnSet, AsyncCallback callback,
											  object asyncState)
			{
				object[] _args = new object[3];
				_args[0] = entityName;
				_args[1] = id;
				_args[2] = columnSet;
				IAsyncResult _result = base.BeginInvoke("Retrieve", _args, callback, asyncState);
				return _result;
			}

			public Entity EndRetrieve(IAsyncResult result)
			{
				object[] _args = new object[0];
				Entity _result = ((Entity)(base.EndInvoke("Retrieve", _args, result)));
				return _result;
			}

			public IAsyncResult BeginUpdate(Entity entity, AsyncCallback callback, object asyncState)
			{
				object[] _args = new object[1];
				_args[0] = entity;
				IAsyncResult _result = base.BeginInvoke("Update", _args, callback, asyncState);
				return _result;
			}

			public void EndUpdate(IAsyncResult result)
			{
				object[] _args = new object[0];
				base.EndInvoke("Update", _args, result);
			}

			public IAsyncResult BeginDelete(string entityName, Guid id, AsyncCallback callback, object asyncState)
			{
				object[] _args = new object[2];
				_args[0] = entityName;
				_args[1] = id;
				IAsyncResult _result = base.BeginInvoke("Delete", _args, callback, asyncState);
				return _result;
			}

			public void EndDelete(IAsyncResult result)
			{
				object[] _args = new object[0];
				base.EndInvoke("Delete", _args, result);
			}

			public IAsyncResult BeginExecute(OrganizationRequest request, AsyncCallback callback, object asyncState)
			{
				object[] _args = new object[1];
				_args[0] = request;
				IAsyncResult _result = base.BeginInvoke("Execute", _args, callback, asyncState);
				return _result;
			}

			public OrganizationResponse EndExecute(IAsyncResult result)
			{
				object[] _args = new object[0];
				OrganizationResponse _result = ((OrganizationResponse)(base.EndInvoke("Execute", _args, result)));
				return _result;
			}

			public IAsyncResult BeginAssociate(string entityName, Guid entityId, Relationship relationship,
											   EntityReferenceCollection relatedEntities, AsyncCallback callback,
											   object asyncState)
			{
				object[] _args = new object[4];
				_args[0] = entityName;
				_args[1] = entityId;
				_args[2] = relationship;
				_args[3] = relatedEntities;
				IAsyncResult _result = base.BeginInvoke("Associate", _args, callback, asyncState);
				return _result;
			}

			public void EndAssociate(IAsyncResult result)
			{
				object[] _args = new object[0];
				base.EndInvoke("Associate", _args, result);
			}

			public IAsyncResult BeginDisassociate(string entityName, Guid entityId, Relationship relationship,
												  EntityReferenceCollection relatedEntities, AsyncCallback callback,
												  object asyncState)
			{
				object[] _args = new object[4];
				_args[0] = entityName;
				_args[1] = entityId;
				_args[2] = relationship;
				_args[3] = relatedEntities;
				IAsyncResult _result = base.BeginInvoke("Disassociate", _args, callback, asyncState);
				return _result;
			}

			public void EndDisassociate(IAsyncResult result)
			{
				object[] _args = new object[0];
				base.EndInvoke("Disassociate", _args, result);
			}

			public IAsyncResult BeginRetrieveMultiple(QueryBase query, AsyncCallback callback, object asyncState)
			{
				object[] _args = new object[1];
				_args[0] = query;
				IAsyncResult _result = base.BeginInvoke("RetrieveMultiple", _args, callback, asyncState);
				return _result;
			}

			public EntityCollection EndRetrieveMultiple(IAsyncResult result)
			{
				object[] _args = new object[0];
				EntityCollection _result = ((EntityCollection)(base.EndInvoke("RetrieveMultiple", _args, result)));
				return _result;
			}

			#endregion
		}

		#endregion
	}

}

